'use strict';
const urllib = require('url');
const path = require('path');

module.exports = appInfo => {
    const config = {};

    const DEBUG = Boolean(process.env.DEBUG);
    config.debug = DEBUG;

    config.keys = appInfo.name + '_1503475727933_2164';

    config.middleware = ['compress', 'appView'];

    config.proxy = true;

    config.crm = {
        webUrl: getOrigin(process.env.CRM_WEB_URL)
    }

    config.baiji = {
        url: process.env.PARTNER_BAIJI_URL,
    }

    config.ads = {
        isShow: process.env.ADS_IS_SHOW,
    }
    config.qiyu = {
        appId: 'SXlXEz226QQo80IYmshr1T9R6TdUEsSf',
        appSecret: 'npm ',
        webUrl: getOrigin(process.env.QIYU_WEB_URL),
    };

    config.pbm = {
        baseUrl: process.env.PBM_API_URL,
        // baseUrl:'https://app.wanhuhealth.com/services/rest/',
    }

    config.baiduTongjiCode = process.env.BAIDU_TONGJI_CODE;

    config.weixin = {
        debug: false,
        appId: process.env.WEIXIN_APP_ID,
        appSecret: process.env.WEIXIN_APP_SECRET,
        webUrl: process.env.WEIXIN_WEB_URL,
        token: process.env.WEIXIN_MESSAGE_TOKEN,
        miniAppId: process.env.WEIXIN_MINI_APP_ID,
        encodingAESKey: process.env.WEIXIN_MESSAGE_ENCODING_KEY,
        verifyFile: process.env.WEIXIN_VERIFY_FILE,
    };

    config.bodyOnDemand = {
        formLimit: '4mb',
    }

    config.baiduMap = {
        appKey: process.env.BMAP_APP_KEY
    };

    config.insurance = {
        sellPackageId: process.env.INSURANCE_PACKAGE_ID,
        // path: process.env.INSURANCE_DOWNLOAD_PATH,
    }

    config.warehouses = {
        warehouseId: process.env.WAREHOUSE_ID,
    }


    config.appView = {
        path: path.resolve(__dirname, '..', '..', 'client', 'build')
    };

    config.api = {
        clients: {
            portal: {
                baseUrl: 'eureka://wanhuhealth-weixin-portal/',
                auth: {
                    type: 'oauth-client',
                    refreshTokenAfter401: true,
                    retryAfterRefresh: true,
                    clientId: 'weixin',
                    clientSecret: 'weixin_secret',
                    scope: 'weixin',
                    accessTokenUri: 'eureka://oauth2-services/oauth/token',
                }
            },
            portalJson: {
                baseUrl: 'eureka://wanhuhealth-weixin-portal/',
                contentType: 'json',
                auth: {
                    type: 'oauth-client',
                    refreshTokenAfter401: true,
                    retryAfterRefresh: true,
                    clientId: 'weixin',
                    clientSecret: 'weixin_secret',
                    scope: 'weixin',
                    accessTokenUri: 'eureka://oauth2-services/oauth/token',
                }
            },
            bind: {
                baseUrl: 'eureka://wanhuhealth-weixin-bind/'
            }
        },
    }

    config.security = {
        domainWhiteList: [getDomain(process.env.QIYU_WEB_URL), getDomain(process.env.WEIXIN_WEB_URL), 'open.weixin.qq.com', 'wanhuhealth.oss-cn-beijing.aliyuncs.com', 'video.shendutv.com','at.alicdn.com','cdn.bootcdn.net','v1.cnzz.com'],
        csrf: {
            ignore: ctx => true
        }
    };

    config.logger = {
        dir: process.env.LOGS_DIR || '/logs'
    };

    config.cors = {
        origin: getOrigin(process.env.QIYU_WEB_URL),
        allowMethods: 'POST',
        credentials: true
    }

    let webUrl = config.weixin.webUrl || 'http://local.eldereal.me:7001/';
    if (webUrl && webUrl[webUrl.length - 1] === '/') {
        webUrl = webUrl.substr(0, webUrl.length - 1);
    }

    return config;
};

function getOrigin(url) {
    if (!url) return undefined;
    const u = urllib.parse(url);
    return u.protocol + '//' + u.host;
}

function getDomain(url) {
    if (!url) return undefined;
    const u = urllib.parse(url);
    return u.host;
}
